import router from "@/util/router"
import {ref} from 'vue';
import { createRouter} from "vue-router";


function loadModule(path) {
    const modules = import.meta.glob("../components/!**!/!*.vue");
    //console.log(modules)
    let module;
    if (modules["../components/" + path + ".vue"]) {
        module = modules["../components/" + path + ".vue"];
    } else {
        //module = modules["../views/error/404.vue"];
    }

    //console.log("path", path);
    //console.log("module", module);

    return module;
}

// 动态加载路由
export async function dynamicAddRoute(menulist){

    //console.log("111======",menulist);
    menulist.forEach(menus=>{
        if (menus.children != ref([])){
            menus.children.forEach(child=>{
                let route = {
                    name: child.path,
                    path: child.path,
                    component: loadModule(child.component)
                }
                router.addRoute("mainpage", route);
            })
        }
    });
    console.log("动态",router);
    router.getRoutes().forEach(route=>{
        console.log("route",route)
    })

}

// 重置路由函数
export function resetRouter() {
    const newRouter = createRouter({
        history: router.history,
        routes: dynamicRouter
    })
    router.matcher = newRouter.matcher // 替换matcher实现重置
}



